package com.test.functiontese;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.camera2.CameraManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

public class IDActivity extends AppCompatActivity {

    private SurfaceView surfaceview;
    private Camera camera;
    private Button take;
    private boolean hasSurface = false;
    private CameraManager cameraManager;
    private Camera.Size mPreviewSize;
    private List<Camera.Size> mSupportedPreviewSizes;

    private int requestedCameraId = -1;

    public CameraManager getCameraManager() {
        return cameraManager;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); // 没有标题  必须在设置布局之前找到调用
        setContentView(R.layout.activity_main2);

            /*
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, // 设置全屏显示
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            */

        take = (Button)findViewById(R.id.take);

        surfaceview = (SurfaceView) findViewById(R.id.surfaceview);
        SurfaceHolder holder = surfaceview.getHolder();
        holder.setFixedSize(176, 155);// 设置分辨率
        holder.setKeepScreenOn(true);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        // SurfaceView只有当activity显示到了前台，该控件才会被创建     因此需要监听surfaceview的创建
        holder.addCallback(new MySurfaceCallback());

        //拍照按钮
        take.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                takepicture();

            }
        });


    }


    //点击事件
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        //对焦
        camera.autoFocus(new Camera.AutoFocusCallback() {
            @Override
            public void onAutoFocus(boolean b, Camera camera) {
                camera.cancelAutoFocus();
            }
        });

        return super.onTouchEvent(event);
    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    /**
     * 监听surfaceview的创建
     * @author Administrator
     *    Surfaceview只有当activity显示到前台，该空间才会被创建
     */
    private final class MySurfaceCallback implements SurfaceHolder.Callback {

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                                   int height) {
            // TODO Auto-generated method stub

        }

        /**
         * 打开摄像头
         *
         * @param cameraId 摄像头id
         * @return Camera
         */
        public Camera open(int cameraId, Context mContext) {
            int nucameras = Camera.getNumberOfCameras();
            if (nucameras == 0) {
                return null;
            }
            boolean explicitRequest = cameraId >= 0;
            if (!explicitRequest) {
                // Select a camera if no explicit camera requested
                int index = 0;
                while (index < nucameras) {
                    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
                    Camera.getCameraInfo(index, cameraInfo);
                    if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                        break;
                    }
                    index++;
                }
                cameraId = index;
            }
            Camera camera;
            if (cameraId < nucameras) {
                camera = Camera.open(cameraId);
            } else {
                if (explicitRequest) {
                    camera = null;
                } else {
                    camera = Camera.open(0);
                }
            }
            mSupportedPreviewSizes = camera.getParameters()
                    .getSupportedPreviewSizes();
            if (mSupportedPreviewSizes != null) {
                mPreviewSize = getOptimalPreviewSize(
                        mSupportedPreviewSizes, getScreenWidth(mContext), getScreenHeight(mContext));
            }
            Log.d("zkcam", "w=" + getScreenWidth(mContext));
            Log.d("zkcam", "h=" + getScreenHeight(mContext));
            Camera.Parameters parameters = camera.getParameters();
            parameters.setPreviewSize(mPreviewSize.width,
                    mPreviewSize.height);
            Log.d("zkcam", "pw=" + mPreviewSize.width);
            Log.d("zkcam", "ph=" + mPreviewSize.height);
            camera.setParameters(parameters);
            return camera;
        }
        public int getScreenWidth(Context context) {
            DisplayMetrics dm = context.getResources().getDisplayMetrics();
            return dm.widthPixels;
        }
        /**
         * 获得屏幕高度
         *
         * @param context 上下文
         * @return 屏幕除去通知栏的高度
         */
        public int getScreenHeight(Context context) {
            DisplayMetrics dm = context.getResources().getDisplayMetrics();
            return dm.heightPixels;
        }
        private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
            final double ASPECT_TOLERANCE = 0.2;
            double targetRatio = (double) w / h;

            if (sizes == null) return null;

            Camera.Size optimalSize = null;
            double minDiff = Double.MAX_VALUE;

            int targetHeight = h;

            for (Camera.Size size : sizes) {
                double ratio = (double) size.width / size.height;
                if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }

            if (optimalSize == null) {
                minDiff = Double.MAX_VALUE;
                for (Camera.Size size : sizes) {
                    if (Math.abs(size.height - targetHeight) < minDiff) {
                        optimalSize = size;
                        minDiff = Math.abs(size.height - targetHeight);
                    }
                }
            }


            return optimalSize;
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            // TODO Auto-generated method stub

//            Camera theCamera = camera;
//            if (theCamera == null){
//                theCamera = open(requestedCameraId,IDActivity.this);
//                camera = theCamera;
//            }
            try {
                camera = Camera.open(requestedCameraId);
                int PreviewWidth = 0;
                int PreviewHeight = 0;
                WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);//获取窗口的管理器
                Display display = wm.getDefaultDisplay();//获得窗口里面的屏幕
                Camera.Parameters parameters  = camera.getParameters();
                // 选择合适的预览尺寸
                List<Camera.Size> sizeList = parameters.getSupportedPreviewSizes();

                // 如果sizeList只有一个我们也没有必要做什么了，因为就他一个别无选择
                if (sizeList.size() > 1) {
                    Iterator<Camera.Size> itor = sizeList.iterator();
                    while (itor.hasNext()) {
                        Camera.Size cur = itor.next();
                        if (cur.width >= PreviewWidth
                                && cur.height >= PreviewHeight) {
                            PreviewWidth = cur.width;
                            PreviewHeight = cur.height;
                            break;
                        }
                    }
                }
                parameters.setPreviewSize(PreviewWidth, PreviewHeight); //获得摄像区域的大小
                parameters.setPreviewFrameRate(3);//每秒3帧  每秒从摄像头里面获得3个画面
                parameters.setPictureFormat(PixelFormat.JPEG);//设置照片输出的格式
                parameters.set("jpeg-quality", 85);//设置照片质量
                parameters.setPictureSize(PreviewWidth, PreviewHeight);
                camera.setParameters(parameters); // 将参数设置给相机
                //右旋90度，将预览调正
                camera.setDisplayOrientation(90);
                // 设置预览显示
                camera.setPreviewDisplay(surfaceview.getHolder());
                // 开启预览
                camera.startPreview();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            if(camera != null){
                camera.release();
                camera = null;
            }
        }

    }



    //拍照的函数
    public void takepicture(){
        /*
         * shutter:快门被按下
         * raw:相机所捕获的原始数据
         * jpeg:相机处理的数据
         */
        camera.takePicture(null, null, new MyPictureCallback());
    }

    //byte转Bitmap
    public Bitmap Bytes2Bimap(byte[] b) {
        if (b.length != 0) {
            return BitmapFactory.decodeByteArray(b, 0, b.length);
        } else {
            return null;
        }
    }

    //bitmap转byte
    public byte[] Bitmap2Bytes(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

    /**
     * 将彩色图转换为黑白图
     *
     */

    //照片回调函数，其实是处理照片的
    private final class MyPictureCallback implements Camera.PictureCallback {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            // TODO Auto-generated method stub
            try {

                Bitmap bitmap = Bytes2Bimap(data);
                Matrix m = new Matrix(); int width = bitmap.getWidth(); int height = bitmap.getHeight(); m.setRotate(90);
                //将照片右旋90度
                bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, m,
                        true);

                Log.d("TAG","width "+width);
                Log.d("TAG","height "+height);

                //截取透明框内照片(身份证)
                Bitmap  bitmap1 = Bitmap.createBitmap(bitmap,50,270,680,450);

                //住址
                Bitmap bitmap2 = Bitmap.createBitmap(bitmap1,90,225,320,125);

                //身份证号
                Bitmap bitmap3 = Bitmap.createBitmap(bitmap1,195,350,400,60);

                //姓名
                Bitmap bitmap4 = Bitmap.createBitmap(bitmap1,105,50,110,55);



                    /*
                    Bundle bundle = new Bundle();
                    bundle.putParcelable("photo", bitmap1);
                    bundle.putParcelable("address", bitmap2);
                    bundle.putParcelable("number", bitmap3);
                    bundle.putParcelable("name", bitmap4);
                    intent.putExtra("bundle", bundle);
                    */




                data = Bitmap2Bytes(bitmap1);
                File file = new File(Environment.getExternalStorageDirectory(),"身份证"+".jpg");
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(data);
                fos.close();


                data = Bitmap2Bytes(bitmap2);
                File file2 = new File(Environment.getExternalStorageDirectory(),"住址"+".jpg");
                FileOutputStream fos2 = new FileOutputStream(file2);
                fos2.write(data);
                fos2.close();

                data = Bitmap2Bytes(bitmap3);
                File file3 = new File(Environment.getExternalStorageDirectory(),"身份证号"+".jpg");
                FileOutputStream fos3 = new FileOutputStream(file3);
                fos3.write(data);
                fos3.close();

                data = Bitmap2Bytes(bitmap4);
                File file4 = new File(Environment.getExternalStorageDirectory(),"姓名"+".jpg");
                FileOutputStream fos4 = new FileOutputStream(file4);
                fos4.write(data);
                fos4.close();

                Intent intent = new Intent(IDActivity.this,ResultActivit.class);
                startActivity(intent);



                // 在拍照的时候相机是被占用的,拍照之后需要重新预览
                //camera.startPreview();
                //跳到新的页面

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }
}
